import pandas as pd
import os
import tarfile
import urllib

import urllib3


pd.set_option('display.width', 1000)  # 适应宽屏显示器
# 设置最大显示行数（默认 60）
pd.set_option('display.max_rows', 100)

# 设置最大显示列数（默认 20）
pd.set_option('display.max_columns', 100)

DOWNLOAD_ROOT = "https://raw.githubusercontent.com/ageron/handson-ml2/master/"
HOUSING_PATH = os.path.join("datasets", "housing")
HOUSING_URL =DOWNLOAD_ROOT +"datasets/housing/housing.tgz"


def fetch_housing_data_lib3(housing_url=HOUSING_URL, housing_path=HOUSING_PATH):
    # 创建目录（如果不存在）
    os.makedirs(housing_path, exist_ok=True)
    tgz_path = os.path.join(housing_path, "housing.tgz")

    # 使用urllib3下载文件
    http = urllib3.PoolManager()
    response = http.request('GET', housing_url)

    # 将下载的内容写入文件
    with open(tgz_path, 'wb') as f:
        f.write(response.data)

    # 解压文件
    housing_tgz = tarfile.open(tgz_path)
    housing_tgz.extractall(path=housing_path)
    housing_tgz.close()

def fetch_housing_data(housing_url=HOUSING_URL, housing_path=HOUSING_PATH):
    os.makedirs(housing_path, exist_ok=True)
    tgz_path = os.path.join(housing_path, "housing.tgz")
    print(tgz_path)
    print(dir(urllib))
    urllib.request.urlretrieve(housing_url, tgz_path)

    housing_tgz = tarfile.open(tgz_path)
    housing_tgz.extractall(path=HOUSING_PATH)
    housing_tgz.close()

def load_housing_data(housing_url=HOUSING_URL):
    csv_path = os.path.join(HOUSING_PATH, "housing.csv")
    return pd.read_csv(csv_path)